﻿namespace CRMG.Container.Source.Helper
{
    using System;
    using System.Data;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;

    public class ControlUtil
    {

        #region [ Set Selected Value Of RadDropDownList ]

        public static void SetSelectedValueOfDropDown(RadDropDownList drpList, object value)
        {
            try
            {

                for (int i = 0; i < drpList.Items.Count; i++)
                {
                    if (drpList.Items[i].Value.ToString().Trim().ToLower().Equals(value.ToString().Trim().ToLower()) == true)
                    {
                        drpList.SelectedIndex = i;
                        break;
                    }
                }
            }
            catch (Exception)
            {
                drpList.SelectedIndex = -1;
            }
        }

        #endregion


        #region [ Get Object With Ref Column And Ref Value ]

        public static object GetObjectWithRefColumnAndRefValue(DataTable dt, string destinationColumn, string refColumn, object refValue)
        {
            try
            {
                object retObj = null;
                foreach (DataRow row in dt.Rows)
                {
                    if (row[refColumn].ToString().Equals(string.Format("{0}", refValue)) == true)
                    {
                        retObj = row[destinationColumn];
                        break;
                    }
                }
                return retObj;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ Set Enable State Some Controls ]

        public static void SetEnableStateSomeControls(bool enableState, params Control[] controls)
        {
            foreach (Control ctrl in controls)
            {
                ctrl.Enabled = enableState;
            }
        }

        #endregion


        #region [ Set Visible State Some Controls ]

        public static void SetVisibleStateSomeControls(bool visibleState, params Control[] controls)
        {
            foreach (Control ctrl in controls)
            {
                ctrl.Visible = visibleState;
            }
        }

        #endregion


        #region [ Load Data Table To Combobox ]

        public static void LoadDataTable2ComboBox(ComboBox cmbx, DataTable dt, string displayMember, string valueMember)
        {
            try
            {
                cmbx.DataSource = null;
                cmbx.DisplayMember = displayMember;
                cmbx.ValueMember = valueMember;
                cmbx.DataSource = dt;
                cmbx.SelectedIndex = -1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ Prepare Data Grid View ]

        public static void PrepareDataGridView(DataGridView gridView, string columnHeaderList, char delimiter, string unvisibleLimiter)
        {
            try
            {
                string[] strList = columnHeaderList.Split(delimiter);

                for (int i = 0; i < strList.Length; i++)
                {
                    gridView.Columns[i].HeaderText = strList[i];

                    if (true == strList[i].Contains(unvisibleLimiter))
                    {
                        gridView.Columns[i].Visible = false;
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ LoadDataTable2ListBoxWithOneColumn ]

        public static void LoadDataTable2ListBoxWithOneColumn(ListBox lstbx, DataTable dt, string column, bool clearAll)
        {
            try
            {
                if (true == clearAll)
                    lstbx.Items.Clear();

                foreach (DataRow row in dt.Rows)
                {
                    if (null != row[column])
                        lstbx.Items.Add(row[column].ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ LoadDataTable2ListBoxWithOneColumn ]

        public static void LoadDataTable2CheckedListBoxWithOneColumn(CheckedListBox chkLstBox, DataTable dt, string column, bool clearAll)
        {
            try
            {
                if (true == clearAll)
                    chkLstBox.Items.Clear();

                foreach (DataRow row in dt.Rows)
                {
                    object obj = row[column];
                    chkLstBox.Items.Add(obj != null ? obj.ToString() : "");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ Show Info Message ]

        public static void ShowInfoMessage(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion


        #region [ Show Warning Message ]

        public static void ShowWarningMessage(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        #endregion


        #region [ Show Error Message ]

        public static void ShowErrorMessage(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        #endregion


        #region [ Show Confirm Dialog ]

        public static DialogResult ShowConfirmDialog(string message, string caption)
        {
            return MessageBox.Show(message, caption, MessageBoxButtons.YesNo);
        }

        #endregion


        #region [ Load DataTable To DropDownList ]

        public static void LoadDataTable2DropDownList(RadDropDownList drpList, DataTable dt, string displayField, string valueField)
        {
            try
            {
                drpList.DataSource = null;
                drpList.DataSource = dt;
                drpList.DisplayMember = displayField;
                drpList.ValueMember = valueField;
                drpList.SelectedIndex = -1;
            }
            catch (Exception)
            {
                throw;
            }
        }


        #endregion

        #region [ Load String To RadDropDownList ]

        public static void LoadStringToDropDownList(RadDropDownList drpList, string sourceStr, char delimiter)
        {
            try
            {
                string[] lst = sourceStr.Split(delimiter);
                drpList.DataSource = lst;
                drpList.SelectedIndex = -1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region [ Set Text Of Text Boxes ]

        public static void SetTextOfTextBoxes(string text, params RadTextBox[] textBoxes)
        {
            foreach (RadTextBox txt in textBoxes)
            {
                txt.Text = text;
            }
        }

        #endregion

        #region [ Set Index Of Drop boxes ]

        public static void SetIndexOfDropboxes(int index, params RadDropDownList[] drpList)
        {
            foreach (RadDropDownList drp in drpList)
            {
                drp.SelectedIndex = index;
            }
        }

        #endregion

    }
}
